package com.snailct.bpp.service.core.commons;

import com.snailct.bpp.commons.protocol.RespResult;
import com.snailct.bpp.commons.utils.JsonUtils;
import com.snailct.bpp.commons.utils.ReflectUtil;
import com.snailct.bpp.commons.utils.SpringContextHolder;
import com.snailct.bpp.commons.xom.castor.XOMTransformerReader;
import com.snailct.bpp.service.core.commons.support.ParamMapUtils;
import com.snailct.bpp.service.core.definition.DefaultServiceDefinition;
import com.snailct.bpp.service.core.definition.ServiceDefinition;
import com.snailct.bpp.service.core.handle.ServiceDefinitionHolder;

import javax.swing.*;
import java.io.IOException;
import java.util.List;

/**
 * 对请求服务前进行数据封装
 */
public class DefaultService implements IService {
    public String execute(String sendData) {
        XOMTransformerReader xomTransformerReader = SpringContextHolder.getBean("xomTransformerReader");
        DefaultServiceDefinition serviceDefinition = null;
        try {
            serviceDefinition=(DefaultServiceDefinition)xomTransformerReader.unMarshal(sendData);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Object[] args = ParamMapUtils.mapServiceParam(serviceDefinition);

        Object result = ReflectUtil.invoke(SpringContextHolder.getBean(serviceDefinition.getComponent()),serviceDefinition.getMethodName(),args);
        RespResult respResult = null;
        String jsonStr = null;
        if(result instanceof List){
            jsonStr = JsonUtils.beanToJsonArray(result);

        }else{


            jsonStr = "操作成功";
        }

        System.out.println(jsonStr);
        return jsonStr;
    }


}
